var seedary;
$(document).ready(function() {
    rnd_str();
    $("#picLoginCaptcha").html("<img src=http://127.0.0.1:8000/captcha/"+seedary+"/>");
	$(".top-logo").css("min-height", $(".top-info").height() + $(".top-title").height() + "px");
	$(".top-title").css("margin-top", $(".top-logo").height() - $(".top-title").height() - 1 + "px");
	if ($(".top-logo").height() - $(".top-title").height() - $(".top-info").height() > 0) {
		$(".top-info").css("margin-top", ($(".top-logo").height() - $(".top-title").height() - $(".top-info").height())/2 + "px");
	}
	$(".top-info").css("min-height", $(".top-info").height() + "px");

    $("#login_username").blur(function() {
        checkLoginUsername();
    });
	$("#login_password").blur(function() {
        checkLoginPassword();
    });
    $("#login_captcha").blur(function() {
        checkLoginCaptcha();
    });
    $("#login_submit").click(function() {
        var loginSuccess = false;
        $.get("http://127.0.0.1:8000/check_login/" + $("#login_username").val() + "/" + $("#login_password").val() + "/", function(data) {
           if (data == "Failure") {
               $("#checkLogin").html("Username and password not match.");
           } else {
               loginSuccess = true;
           }
        });
        if (!checkLoginCaptcha()) {
            rnd_str();
            $("#picLoginCaptcha").html("<img src=http://127.0.0.1:8000/captcha/"+seedary+"/>");
        }
        else {
            if (!checkLoginUsername() && !checkLoginPassword()) {
                alert("Enter your username and password!");
            } else if (!checkLoginUsername()) {
                alert("Enter your username!");
            } else if (!checkLoginPassword()) {
                alert("Enter your password!");
            }
            $("#login_submit").val("Loading...");
            $("#login_submit").css("color", "gray");
            window.setTimeout(function() {
                $.ajax({
                type: "POST",
                url: "/login/",
                data: "username=" + $("#login_username").val() + "&password=" + $("#login_password").val(),
                success: function() {
                    $("#login_submit").val("Login");
                    $("#login_submit").css("color", "rgb(0,0,0)");
                    if (loginSuccess) {
                        self.location = "/home/";
                    }
                }});
            }, 1000);
        }
	});

    $("#register_username").blur(function() {
        $("#checkRegisterUsername").html("");
        if (checkRegisterUsername()) {
            $.get("http://127.0.0.1:8000/is_user_exist/" + $("#register_username").val() + "/", function(data) {
                if (data == "True") {
                    $("#checkRegisterUsername").html("\"" + $("#register_username").val() + "\" has been used.");
                }
            });
        }
    });
    $("#register_password").blur(function() {
       checkRegisterPassword(); 
    });
    $("#register_confirm_password").blur(function() {
        checkRegisterConfirmPassword();
    });
    $("#register_email").blur(function() {
        checkRegisterEmail();
    });
    $("#register_submit").click(function() {
        if (!checkRegisterUsername() && !checkRegisterPassword() && !checkRegisterConfirmPassword() && !checkRegisterEmail()) {
            alert("not valid!");
        }
    })
});

function checkLoginUsername() {
    $("#checkLoginUsername").html("");
    if (!$("#login_username").val().match(/^[a-zA-Z0-9_]*$/)) {
        $("#checkLoginUsername").html("Only alphabet, number, underline is allowed");
        return false;
    }
    if ($("#login_username").val() == "") {
		$("#checkLoginUsername").html("Enter your username.");
        return false;
	}
    return true;
}

function checkLoginPassword() {
    $("#checkLoginPassword").html("");
    if ($("#login_password").val() == "") {
		$("#checkLoginPassword").html("Enter your password.");
        return false;
	}
    return true;
}

function checkRegisterUsername() {
    $("#checkRegisterUsername").html("");
    if (!$("#register_username").val().match(/^[a-zA-Z0-9_]*$/)) {
        $("#checkRegisterUsername").html("Only alphabet, number, underline is allowed");
        return false;
    }
    if ($("#register_username").val() == "") {
		$("#checkRegisterUsername").html("Username can not be empty.");
        return false;
	}
    return true;
}

function checkRegisterPassword() {
    $("#checkRegisterPassword").html("");
    if ($("#register_password").val() == "") {
		$("#checkRegisterPassword").html("Password can not be empty.");
        return false;
	}
    return true;
}

function checkRegisterConfirmPassword() {
    $("#checkRegisterConfirmPassword").html("");
    if ($("#register_confirm_password").val() == "") {
		$("#checkRegisterConfirmPassword").html("Confirm your password.");
        return false;
	}
    if ($("#register_confirm_password").val() != $("#register_password").val()) {
        $("#checkRegisterConfirmPassword").html("Password not match.");
        return false;
    }
    return true;
}

function checkRegisterEmail() {
    $("#checkRegisterEmail").html("");
    if (!$("#register_email").val().match(/^[a-zA-Z0-9_.@]*$/)) {
        $("#checkRegisterEmail").html("Only alphabet, number, underline, dot, \"@\" is allowed");
        return false;
    }
    if ($("#register_email").val() == "") {
		$("#checkRegisterEmail").html("Email can not be empty.");
        return false;
	}
    if ($("#register_email").val().indexOf("@") == -1) {
        $("#checkRegisterEmail").html("Email should contain \"@\".");
        return false;
    }
    if ($("#register_email").val().substr($("#register_email").val().indexOf("@") + 1).indexOf("@") != -1) {
        $("#checkRegisterEmail").html("Email should contain only one \"@\".");
        return false;
    }
    if ($("#register_email").val().substr($("#register_email").val().indexOf("@")).indexOf(".") == -1) {
        $("#checkRegisterEmail").html("Email's domain should contain \".\".");
        return false;
    }
    if ($("#register_email").val().indexOf(".") == 0 || $("#register_email").val().substr(0, $("#register_email").val().indexOf("@")).lastIndexOf(".") == $("#register_email").val().substr(0, $("#register_email").val().indexOf("@")).length - 1) {
        $("#checkRegisterEmail").html("Email's name should not start or end with \".\".");
        return false;
    }
    if ($("#register_email").val().substr($("#register_email").val().indexOf("@")).indexOf(".") == 1 || $("#register_email").val().lastIndexOf(".") == $("#register_email").val().length - 1) {
        $("#checkRegisterEmail").html("Email's domain should not start or end with \".\".");
        return false;
    }
    return true;
}

function checkLoginCaptcha() {
     $("#checkLoginCaptcha").html("");
    if ($("#login_captcha").val() == "") {
		$("#checkLoginCaptcha").html("Enter your captcha.");
        return false;
	}
    if ($("#login_captcha").val() != seedary) {
		$("#checkLoginCaptcha").html("Captcha error.");
        return false;
	}
    return true;
}

function rnd_str() {
	var Seed_array = new Array();
	var i;
	Seed_array = "0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".split(" ");
	seedary = ""
	for(i=0; i<4; i++) {
		seedary += Seed_array[Math.round(Math.random()*(Seed_array.length-1))]
	}
}